Ext.require([
    'Ext.tree.*',
    'Ext.data.*',
    'Ext.window.MessageBox'
]);
Ext.define('Module.Privilege.RolePrivilege', {
	extend : 'Ext.panel.Panel',
	alias : 'widget.roleprivilege',
	layout: 'border',
	initComponent : function() {
		var me = this;
		me.defaults = {padding: 1};
		var role = Ext.create('Ext.grid.Panel',{
			title: '系统角色',
			region: 'west',
			width: 600,
//			border: false,
			region: 'west',
			tbar: [{
				text: '添加角色',
				iconCls: 'icon-add',
				handler: function(){
					me.addRole();
				}
			},{
				text: '修改角色',
				iconCls: 'icon-user_edit',
				handler: function(){
					var rec = role.getSelectionModel().getSelection()[0];
					if(rec){
						me.addRole(rec);
						Ext.getCmp(me.id+'-roleWin').setIconCls('icon-user_edit');
					}else{
						Ext.Msg.alert('提示', '请选择要修改的角色！');
					}
				}
			},{
				text: '删除角色',
				iconCls: 'icon-delete',
				handler: function(){
					me.delRole(role);
				}
			},{
				xtype: 'textfield',
				labelWidth: 65,
				width: 165,
				fieldLabel: '角色名称',
				maxLength:16,
				listeners: {
					change: function(f,v){
						role.store.proxy.extraParams.cd = v;
					}
				}
			},{
				iconCls: 'icon-search',
				text: '查询',
				handler: function(){
					role.store.load();
				}
			}],
			store: Ext.create('Ext.data.Store', {
				autoLoad: true,
				fields: ['id','name','describe','enbale','createtime','updatetime','updateuser'],
		        proxy: {
		            type: 'ajax',
		            reader: 'json',
		            url: 'priv/role/getRole.do'
		        }
		    }),
		    columns: [{
		    	text: 'ID',
		    	dataIndex: 'id',
		    	width: 80,
		    	hidden:true
		    },
		    {xtype: 'rownumberer',text:'编号',width:35}
		    ,{
		    	text: '角色名称',
		    	dataIndex: 'name',
		    	flex: 1
		    },{
		    	text: '角色描述',
		    	dataIndex: 'describe',
		    	flex: 1,
		    	renderer: function(v){
		    		return '<span title="'+v+'">'+v+'</span>';
		    	}
		    },{
		    	text: '更新时间',
		    	dataIndex: 'updatetime',
		    	hidden: true,
		    	flex: 1.5,
		    	renderer: function(v){
		    		return Ext.Date.format(new Date(v),'Y-m-d h:m:s');
		    	}
		    },{
		    	text: '更新用户',
		    	dataIndex: 'updateuser',
		    	hidden: true,
		    	flex: 1
		    }],
		    listeners: {
		    	select: function(model,rec){
		    		menutree.store.proxy.extraParams.role=rec.data.id;
		    		menutree.store.load();
		    		menutree.setTitle('['+rec.data.name+']---资源权限分配');
		    	}
		    }
		});
		
		var menutree = me.menutree =  Ext.create('Ext.tree.Panel',{
			rootVisible: false,
			title: '资源权限分配',
			region: 'center',
			widht: 200,
			store: Ext.create('Ext.data.TreeStore', {
				fields: ['id','index','link','pid','text','linktype',{name: 'checked',type: 'boolean'}],
		        proxy: {
		            type: 'ajax',
		            url: 'priv/role/getMenu.do'
		        },
		        root: {
		            id: '0'
		        },
		        listeners: {
		        	load: function(){
//		        		menutree.expandAll(function(){
//			    			menutree.collapseAll();
//			    		});
		        	}
		        }
		    }),
		    tbar: [{
		    	text: '全选',
		    	iconCls: 'icon-action_check',
		    	handler: function(){
		    		menutree.getRootNode().cascadeBy(function(n){
	    				n.set('checked',true);
	    			});
		    	}
		    },{
		    	text: '全不选',
		    	iconCls: 'icon-stopplay',
		    	handler: function(){
		    		menutree.getRootNode().cascadeBy(function(n){
	    				n.set('checked',false);
	    			});
		    	}
		    },{
		    	text: '刷新',
		    	iconCls: 'icon-arrow_refresh',
		    	handler: function(){
		    		menutree.store.load();
		    	}
		    },{
		    	text: '保存权限',
		    	iconCls: 'icon-save',
		    	handler: function(){
		    		var roleid = menutree.store.proxy.extraParams.role;
		    		if(roleid && '' != roleid){
		    			var checkeds = [];
			    		Ext.each(menutree.getChecked(), function(node) {
			    			checkeds.push(node.data.id);
			    		});
		    			Ext.Ajax.request({
						    url: 'priv/role/addPriv.do',
						    params: {
						    	role: roleid,
						        ids: checkeds.join(',')
						    },
						    success: function(response){
	        	    	    	var rs = Ext.decode(response.responseText);
	        	    	    	if(rs.rst){
	        	    	    		Ext.Msg.alert('提示', '保存成功！');
	        	    	    	}
	        	    	    	else{
	        	    	    		Ext.Msg.alert('提示', '保存失败！详情：<br>'+rs.msg);
	        	    	    	}
	        	    	    }
						});
		    		}
		    		else{
		    			Ext.Msg.alert('提示', '保存失败，没有要保存的角色权限，请选择权限。');
		    		}
		    	}
		    }],
		    listeners: {
		    	checkchange: function(node,checked){
		    		if(checked){
		    			checkAllParent(node);
		    			node.cascadeBy(function(n){
		    				n.set('checked',true);
		    			});
		    			if(node.parentNode){
		    				node.parentNode.set('checked',true);
		    			}
		    		}
		    		else if(!checked){
		    			node.cascadeBy(function(n){
		    				n.set('checked',false);
		    			});
		    		}
		    	}
		    }
		});
		
		me.items=[role,menutree];
		
		
		me.callParent();
	},
	addRole: function(rec){
		var me = this;
		Ext.create('Ext.window.Window',{
			width: 431,
			height: 189,
			iconCls: 'icon-add',
			modal: true,
			id:me.id+'-roleWin',
			layout: 'fit',
			title: rec?'编辑角色信息':'添加角色信息',
			buttons: [{
				text: '保存',
				handler: function(){
					var formp = this.up('panel').items.items[0];
        	    	var form = formp.getForm();
        	    	if(form.isValid()){
        	    		form.submit({
            	    	    url: 'priv/role/addRole.do',
            	    	    success: function(form, action) {
            	    	    	var rs = Ext.decode(action.response.responseText);
            	    	    	if(rs.rst){
            	    	    		Ext.Msg.alert('提示', rs.msg);
            	    	    		formp.up('window').close();
            	    	    		me.items.items[0].store.load();
            	    	    	}
            	    	    	else{
            	    	    		Ext.Msg.alert('提示', '保存失败！详情：<br>'+rs.msg);
            	    	    	}
            	    	    }
            	    	});
        	    	}
        	    	else{
        	    		Ext.Msg.alert('提示', '无效的角色信息,请正确填写角色信息！');
        	    	}
				}
			},{
				text: '取消',
				handler: function(){
					this.up('window').close();
				}
			}],
			items: me.getForm({
				bodyPadding: 20,
				border: false,
				defaults: {anchor: '100%'},
				listeners: {
					afterrender: function(formp){
						if(rec)formp.form.setValues(rec.data);
						for ( var i = 3; i < formp.items.items.length; i++) {
							formp.items.items[i].setVisible(false);
							formp.items.items[i].setDisabled(true);
						}
					}
				}
			})
		}).show();
	},
	delRole: function(grid){
		var me = this;
		var rec = grid.getSelectionModel().getSelection()[0];
		if(rec){
			Ext.Msg.confirm('提示', '你确定要删除当前角色吗？',function(b){
				if('yes' == b){
					Ext.Ajax.request({
					    url: 'priv/role/deleteRole.do',
					    params: {
					        id: rec.data.id,
					        name:rec.data.name
					    },
					    success: function(response){
					    	Ext.Msg.alert('提示', '删除成功！');
					    	grid.store.load();
					    	me.menutree.store.load();
					    }
					});
				}
			});
		}
		else{
			Ext.Msg.alert('提示', '请选择要删除的角色！');
		}
	},
	getForm: function(config){
		return Ext.apply({
			xtype: 'form',
			bodyPadding: 10,
			items: [{
				xtype: 'hidden',
				name: 'id'
			},{
				xtype: 'textfield',
				name: 'name',
				fieldLabel: '角色名称',
				allowBlank: false,
				regex: /^[a-zA-Z0-9\u4e00-\u9fa5]+$/,
				regexText : '不能输入特殊字符',
				maxLength:16
			},{
				xtype: 'textarea',
				name: 'describe',
				fieldLabel: '角色描述',
				maxLength:120
			},{
				xtype: 'textfield',
				name: 'id',
				name: 'createtime',
				fieldLabel: '创建时间'
			},{
				xtype: 'textfield',
				name: 'id',
				name: 'updatetime',
				fieldLabel: '修改时间'
			},{
				xtype: 'textfield',
				name: 'id',
				name: 'updateuser',
				fieldLabel: '修改用户'
			}]
		},config);
	}
});